package com.lqk.asyncfilechannel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

public class AsyncFileChannelDemo {

    private static void writeAsyncFileComplate() throws Exception{
        // 1 创建 AsynchronousFileChannel
        Path path = Paths.get("f:\\lqk\\002.txt");
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        // 2 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("lqkyyds".getBytes());
        buffer.flip();
        // 3 写数据
        fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {
                System.out.println("bytes written："+result);
            }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {

            }
        });
        System.out.println("write over!");
    }

    private static void writeAsyncFileFuture() throws Exception{
        // 1 创建 AsynchronousFileChannel
        Path path = Paths.get("f:\\lqk\\002.txt");
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        // 2 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("lqkyyds".getBytes());
        buffer.flip();
        // 3 写数据
        Future<Integer> future = fileChannel.write(buffer, 0);
        while (!future.isDone());
        buffer.clear();
        System.out.println("write over!");
    }

    private static void readAsyncFileChannelComplate() throws Exception {

        // 1 创建 AsynchronousFileChannel
        Path path = Paths.get("f:\\lqk\\002.txt");
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        // 2 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // 3 调用 channel 的 read 方法得到 Future
        fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {
                System.out.println("result: "+result);
                attachment.flip();
                byte[] data = new byte[attachment.limit()];
                attachment.get(data);
                System.out.println(new String(data));
                attachment.clear();

            }
            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {
                System.out.println("1111111111111111");
            }
        });
        System.out.println("2222222222222222222");
    }

    private static void readAsyncFileChannelFuture() throws Exception {

        // 1 创建 AsynchronousFileChannel
        Path path = Paths.get("f:\\lqk\\002.txt");
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        // 2 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // 3 调用 channel 的 read 方法得到 Future
        Future<Integer> future = fileChannel.read(buffer, 0);
        // 4 判断是否已经完成 isDone，返回 TRUE 表示完成
        while (!future.isDone());
        // 5 读取数据到 buffer 中
        buffer.flip();
//        while (buffer.remaining()>0){
//            System.out.println(buffer.get());
//        }
        byte[] data = new byte[buffer.limit()];
        buffer.get(data);
        System.out.println(new String(data));

        buffer.clear();

    }


    public static void main(String[] args) throws Exception {
//        AsyncFileChannelDemo.readAsyncFileChannelFuture();
//        AsyncFileChannelDemo.readAsyncFileChannelComplate();
//        AsyncFileChannelDemo.writeAsyncFileFuture();
        AsyncFileChannelDemo.writeAsyncFileComplate();
    }
}
